package com.qiall.project.service;

import com.qiall.assertutil.Assert;
import com.qiall.common.entity.PageQuery;
import com.qiall.common.entity.PageResponse;
import com.qiall.common.exception.BusinessResult;
import com.qiall.common.request.BaseRequest;
import com.qiall.common.request.GetByIdRequest;
import com.qiall.project.ability.TaskService;
import com.qiall.project.ability.request.AddOrUpdateTaskRequest;
import com.qiall.project.ability.request.TaskDTO;
import com.qiall.project.ability.request.TaskInfoGetByPageRequest;
import com.qiall.project.data.entity.TaskInfo;
import com.qiall.project.data.mapper.TaskInfoMapper;
import com.qiall.project.service.taskinfo.AddTaskCheckActivity;
import com.qiall.project.service.taskinfo.TaskTrans;
import com.qiall.user.ability.entity.UserInfoDTO;
import com.qiall.user.ability.service.user.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴金辉
 */
@DubboService
@Component
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;
    @Autowired
    private TaskTrans taskTrans;

    @Autowired
    private AddTaskCheckActivity addTaskCheckActivity;

    @DubboReference
    private UserService userService;

    @Override
    public BusinessResult<Void> addOrUpdateTaskInfo(AddOrUpdateTaskRequest request) {
        addTaskCheckActivity.handler(request, null);
        TaskInfo taskInfo = taskTrans.trans(request);
        if (request.getId() == null) {
            taskInfoMapper.insert(taskInfo);
        } else {
            taskInfo.setCreatorId(null);
            taskInfoMapper.update(taskInfo);
        }
        return BusinessResult.success(null);
    }

    @Override
    public BusinessResult<Void> deleteTaskInfo(GetByIdRequest request) {
        taskInfoMapper.delete(request.getId());
        return BusinessResult.success(null);
    }

    @Override
    public BusinessResult<TaskDTO> getTaskInfo(GetByIdRequest request) {
        TaskInfo taskInfo = taskInfoMapper.select(request.getId());
        TaskDTO taskDTO = taskTrans.trans(taskInfo);
        return BusinessResult.success(taskDTO);
    }

    @Override
    public BusinessResult<PageResponse<TaskDTO>> getByPage(TaskInfoGetByPageRequest request) {
        PageQuery pageQuery = request.getPageQuery();
        TaskDTO taskDTO = request.getTaskDTO();
        TaskInfo taskInfo = taskTrans.trans(taskDTO);
        BaseRequest baseRequest=new BaseRequest();
        baseRequest.setAccessToken(request.getAccessToken());
        BusinessResult<UserInfoDTO> userInfoDTOBusinessResult = userService.getUserInfoByAuthCode(baseRequest);
        Assert.notNull(userInfoDTOBusinessResult.getBody(), "用户未登录");
        taskInfo.setOrgId(userInfoDTOBusinessResult.getBody().getOrgId());
        int count = taskInfoMapper.count(taskInfo, pageQuery.getStartTime(), pageQuery.getEndTime());
        if (count == 0) {
            return BusinessResult.success(PageResponse.nullValue(pageQuery));
        }
        List<TaskInfo> taskInfoList
                = taskInfoMapper.queryByPage(taskInfo, pageQuery.getStartTime(), pageQuery.getEndTime(), pageQuery.offset(), pageQuery.limit());
        List<TaskDTO> taskDTOS = taskInfoList.stream().map(x -> taskTrans.trans(x)).collect(Collectors.toList());
        PageResponse pageResponse = PageResponse.withValue(pageQuery, count, taskDTOS);
        return BusinessResult.success(pageResponse);

    }
}
